package com.hxf.web.action.admin;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.hxf.common.PublicUtil;
import com.hxf.dto.AdminUserDto;
import com.hxf.dto.RestaurantDto;
import com.hxf.dto.ReturnData;
import com.hxf.dto.UserDto;
import com.hxf.exception.ServiceException;
import com.hxf.web.action.WebActionBase;

/**
 * 餐厅管理
 * @author tengyicheng
 */
@Results({@Result(name = "success", location = "../common/success.jsp"), @Result(name = "error", location = "../common/error.jsp"), @Result(name = "msg", location = "../common/msg.jsp")})
public class RestaurantAction extends WebActionBase{

	private static final long serialVersionUID = 1L;
	private List<RestaurantDto> restaurantDtoList;
	private String restaurantIds;
	private Integer status;
	private Boolean isRecommend;
	public String name;

	/**
	 * 餐厅信息列表
	 * @return
	 */
	public String restaurantList() {
		try {
			//ReturnData<RestaurantDto>  result = super.serviceFacade.getRestaurantService().findByRestaurantAllPage(targetPage, pageSize);
			RestaurantDto restaurantDto = new RestaurantDto();
			
			if(PublicUtil.isNotNullAndEmpty(name)){
				restaurantDto.setName(name);
			}
			
			//UserDto userDto = (UserDto)getSession().getAttribute("userDto");
			//restaurantDto.setUserId(userDto.getUserId());
			//Object adminUserDto = getSession().getAttribute("adminUserDto"); //联盟管理员Session
			Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session
			
			if(null != restaurentUserDto){
				//AdminUserDto adminUserDto = (AdminUserDto)getSession().getAttribute("adminUserDto");
				restaurantDto.setUserId(getRestaurentUserDto().getId());
			}else{
				restaurantDto.setUserId(getAdminUser().getId());
			}
			
			ReturnData<RestaurantDto>  result = super.serviceFacade.getRestaurantService().findByRestaurantSelectName(restaurantDto,targetPage, pageSize);
			this.setRecordCount(result.getRecordcount());
			restaurantDtoList = result.getResultlist();
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return "restaurant-list";
	}
	
	/**
	 * 审核通过或审核不通过
	 * @return
	 */
	public String audit() {
		try {
			int result = super.serviceFacade.getRestaurantService().updateRestaurant(restaurantIds, status);
			println(result);
		} catch (ServiceException e) { 
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 推荐餐厅和取消推荐
	 * @return
	 */
	public String recommend() {
		try {
			int result = super.serviceFacade.getRestaurantService().updateRestaurantRecommend(restaurantIds, isRecommend);
			println(result);
		} catch (ServiceException e) { 
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		return null;
	}
	
	/**
	 * 作废餐厅和取消作废
	 * @return
	 */
	public String invalid() {
		try {
			int result = super.serviceFacade.getRestaurantService().updateRestaurant(restaurantIds, status);
			println(result);
		} catch (ServiceException e) { 
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		return null;
	}
	
	public List<RestaurantDto> getRestaurantDtoList() {
		return restaurantDtoList;
	}

	public void setRestaurantDtoList(List<RestaurantDto> restaurantDtoList) {
		this.restaurantDtoList = restaurantDtoList;
	}

	public String getRestaurantIds() {
		return restaurantIds;
	}

	public void setRestaurantIds(String restaurantIds) {
		this.restaurantIds = restaurantIds;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Boolean getIsRecommend() {
		return isRecommend;
	}

	public void setIsRecommend(Boolean isRecommend) {
		this.isRecommend = isRecommend;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	 
}
